package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.activity.homepage.MyAttentionActivity;
import com.wenyu.wenyuyuedu.adapter.personage.MyCollectAdapter;
import com.wenyu.wenyuyuedu.bean.homepager.CollectInvitaionBean;
import com.wenyu.wenyuyuedu.bean.personage.MyCollectBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.homepager.CollectInvitationServer;
import com.wenyu.wenyuyuedu.server.personage.MyCollectServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*我的收藏*/
public class MyCollectActivity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.member_left)
    ImageView memberLeft;
    @InjectView(R.id.lv_delete)
    ListView lvDelete;
    private Intent intent;
    private Activity activity;
    private MyCollectAdapter adapter;
    private List<MyCollectBean.InfoBean> info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        StatusBarUtil.setStatusBar(MyCollectActivity.this, R.color.white);
        this.activity = MyCollectActivity.this;
        ButterKnife.inject(this);
        /*条目my_collect_item*/
        initView();
        requestView();
        adapter = new MyCollectAdapter(MyCollectActivity.this);
        lvDelete.setAdapter(adapter);
        adapter.setDeleteOnClickLis(new MyCollectAdapter.deleteOnclickListener() {
            @Override
            public void onClick(int position, View view) {
                switch (view.getId()) {
                    /*条目的点击事件*/
                    case R.id.tv:
                        intent.setClass(activity, MyAttentionActivity.class);
                        MyCollectBean.InfoBean infoBean1 = info.get(position);
                        String title = infoBean1.getTitle();
                        String invitationId = infoBean1.getId();
                        String nickname = infoBean1.getCircle_creator();
                        intent.putExtra("allattentionnickname", nickname);
                        intent.putExtra("allattentionid", invitationId);
                        intent.putExtra("allattentiontitle", title);
                        startActivity(intent);
                        MyToast.showToast(MyCollectActivity.this, "当前的条目", Toast.LENGTH_SHORT);
                        break;
                    /*删除*/
                    case R.id.btn_delete:
                        SwipeMenuLayout parent = (SwipeMenuLayout) view.getParent();
                        parent.quickClose();
                        MyCollectBean.InfoBean infoBean = info.get(position);
                        String id = infoBean.getId();
                        CancleCollect(id, position);
                        break;
                }
            }
        });
    }

    /*删除帖子----取消对帖子的收藏*/
    private void CancleCollect(String id, final int position) {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        CollectInvitationServer collectInvitationServer = ApiWrapper.getInstance().create(CollectInvitationServer.class);
        Call<CollectInvitaionBean> collectBean = collectInvitationServer.getCollectBean(token, id, String.valueOf(2), String.valueOf(2));
        collectBean.enqueue(new Callback<CollectInvitaionBean>() {
            @Override
            public void onResponse(Call<CollectInvitaionBean> call, Response<CollectInvitaionBean> response) {
                CollectInvitaionBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    String msg = body.getMsg();
                    if (code == 1) {
                        MyToast.showToast(activity, "删除成功", Toast.LENGTH_SHORT);
                        adapter.getList().remove(position);
                        adapter.notifyDataSetChanged();
                    } else if (code == 0) {
                        showLoginDoalog();
                    } else if (code == 2500) {
                        MyToast.showToast(activity, msg, Toast.LENGTH_SHORT);
                    } else if (code == 2505) {
                        MyToast.showToast(activity, msg, Toast.LENGTH_SHORT);
                    } else if (code == 2506) {
                        MyToast.showToast(activity, msg, Toast.LENGTH_SHORT);
                    } else if (code == 2507) {
                        MyToast.showToast(activity, msg, Toast.LENGTH_SHORT);
                    }
                }
            }

            @Override
            public void onFailure(Call<CollectInvitaionBean> call, Throwable t) {
                MyToast.showToast(activity, "网络异常，请稍后重试！", Toast.LENGTH_SHORT);
            }
        });
    }

    /*获取数据*/
    private void requestView() {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        MyCollectServer myCollectServer = ApiWrapper.getInstance().create(MyCollectServer.class);
        Call<MyCollectBean> myCollect = myCollectServer.getMyCollect(token);
        myCollect.enqueue(new Callback<MyCollectBean>() {
            @Override
            public void onResponse(Call<MyCollectBean> call, Response<MyCollectBean> response) {
                MyCollectBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    String msg = body.getMsg();
                    if (code == 1) {
                        info = body.getInfo();
                        adapter.addAll(info);
                    } else if (code == 0) {
                        showLoginDoalog();
                    }
                }
            }

            @Override
            public void onFailure(Call<MyCollectBean> call, Throwable t) {

            }
        });
    }

    /*token错误或失效*/
    AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    private void showLoginDoalog() {

        ad = new AlertDialog.Builder(activity).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbSharedUtil.putString(context, SharedConstants.USER_TOKEN, "");
                intent.setClass(activity, LoginActivity.class);
                startActivity(intent);
                ad.dismiss();
            }
        });
    }

    /*初始化控件*/
    private void initView() {
        intent = new Intent();
        memberLeft.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*返回*/
            case R.id.member_left:
                finish();
                break;
        }
    }
}
